//
//  RWNetworkRequest.h
//  RWNetwork
//
//  Created by Ray on 2024/4/25.
//  Copyright © 2024 81SK. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "RWNetworkDefines.h"
#import "RWNetworkCache.h"

NS_ASSUME_NONNULL_BEGIN

@interface RWNetworkRequest : NSObject

/// 请求预处理器
@property (nonatomic, weak) NSObject<RWNetworkRequestIntender> *intender;
/// 请求拦截器
@property (nonatomic, weak) id<RWNetworkInterceptor> interceptor;
/// 请求完成回调代理
@property (nonatomic, weak) id<RWNetworkCompletionDelegate> delegate;
/// 请求过程状态回调代理
@property (nonatomic, weak) id<RWNetworkShowHUDCaller> showHUDCaller;
/// 请求返回数据校验器
@property (nonatomic, weak) id<RWNetworkValidator> validator;


/// 进度回调
@property (nonatomic, strong, readonly) RWNetProgressBlock progres;
/// 请求成功回调
@property (nonatomic, strong, readonly) RWNetSuccessBlock success;
/// 请求失败回调
@property (nonatomic, strong, readonly) RWNetFailureBlock failure;
/// 请求取消回调
@property (nonatomic, strong, readonly) RWNetCancelBlock cancelb;


/// 返回的任务对象
@property (nonatomic, strong) NSURLSessionTask *task;

/// 缓存处理器:只读的，配置缓存策略
@property (nonatomic, strong, readonly) RWNetworkCache *cacheHandler;

/// 重复调用此方法。按掉用的先后顺序执行。在请求完成前不会发起下一次请求
/// @param progress    当前网络请求的进程
/// @param success      当前网络请求的成功回调
/// @param failure      当前网络请求的失败回调
- (void)synchrousStarWithPrgress:(RWNetProgressBlock)progress success:(RWNetSuccessBlock)success failure:(RWNetFailureBlock)failure;

/// 获取网络请求得到的数据所有的Block参数如不需要传入 nil
/// @param progress    当前网络请求的进程
/// @param success      当前网络请求的成功回调
/// @param failure      当前网络请求的失败回调
- (void)startWithPrgress:(RWNetProgressBlock)progress success:(RWNetSuccessBlock)success failure:(RWNetFailureBlock)failure;

/// 暂停的操作
- (void)suspend;

/// 恢复的操作
- (void)resume;

/// 取消的操作
- (void)cancel;

/// 请求发起之前调用（eg. 修改数据缓存策略等）
- (void)beginAFRequest;

/// 请求完成后调用（回调之前）
- (void)afterAFRequest;


#pragma mark 内部拦截器相关方法
/*
 拦截器/拦截器的功能可以由子类通过继承实现，也可以由其它对象实现,两种做法可以共存
 当两种情况共存的时候，子类重载的方法一定要调用一下super.
 super在前:先调用外部拦截器,后调用内部子类重载
 super在后:先调用内部子类重载,在调用外部拦截器
 */

/// 将要发起请求拦截
- (BOOL)shouldCallAPIWithParams:(id)params;//start调用时所在线程
/// 已发起请求拦截
- (void)afterCallingAPIWithParams:(id)params;//start调用时所在线程

/// 数据返回拦截器=>（可以对返回的数据进行处理）
- (void)didReceiveResponse:(RWNetworkResponse *)response;//子线程

- (BOOL)beforePerformSuccessWithResponse:(RWNetworkResponse *)response;//子线程
- (void)afterPerformSuccessWithResponse:(RWNetworkResponse *)response;//start调用时所在线程

- (BOOL)beforePerformFailWithResponse:(RWNetworkResponse *)response;//子线程
- (void)afterPerformFailWithResponse:(RWNetworkResponse *)response;//start调用时所在线程

/// 内部验证器默认实现,子类可覆盖
/// @param errorInfo 验证失败后，用于自定义Code和错误提示
- (BOOL)validateResponse:(RWNetworkResponse *)response errorInfo:(RWNetFailureInfoBlock)errorInfo;

@end

NS_ASSUME_NONNULL_END
